package com.up.vms.interfaces.rest.controller;

import com.up.vms.interfaces.rest.request.SystemUserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.up.vms.application.service.SystemUserService;
import com.up.vms.domain.vm.entity.SystemUser;
import com.up.vms.infrastructure.mvc.BaseResponse;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.interfaces.rest.response.CommonResponse;

@Api("后台用户管理接口")
@RestController
@RequestMapping(value = "/system/users")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;


    @PutMapping(value = "/save")
    @ApiOperation("用户保存接口")
    public BaseResponse save(@RequestBody SystemUserRequest request) {

        CommonResponse commonResponse = new CommonResponse();
        try {
            SystemUser systemUser = new SystemUser();
            systemUser.setUsername(request.getUsername());
            systemUser.setId(request.getId());
            systemUser.setNickName(request.getNickName());
            systemUser.setStatus(1);
            systemUser.setCreator(request.getCreator());
            String password = request.getPassword();
            systemUser.setPassword(new BCryptPasswordEncoder().encode(password));
            GeneRespDTO geneRespDTO = systemUserService.createUser(systemUser);


            commonResponse.setRespCode(geneRespDTO.getRespCode());
        } catch (Exception ex) {
            commonResponse.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }
        return commonResponse;
    }
}

